package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.ServicesDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IDeviceDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.jms.Message;

/**
 * 设备数据Service业务层处理
 * 
 * @author 张三
 * @date 2025-07-05
 */
@Slf4j
@Service
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements IDeviceDataService
{
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 查询设备数据
     * 
     * @param id 设备数据主键
     * @return 设备数据
     */
    @Override
    public DeviceData selectDeviceDataById(Long id)
    {
        return deviceDataMapper.selectById(id);
    }

    /**
     * 查询设备数据列表
     * 
     * @param deviceData 设备数据
     * @return 设备数据
     */
    @Override
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData)
    {
        return deviceDataMapper.selectDeviceDataList(deviceData);
    }

    /**
     * 新增设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    @Override
    public int insertDeviceData(DeviceData deviceData)
    {
        return deviceDataMapper.insert(deviceData);
    }

    /**
     * 修改设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    @Override
    public int updateDeviceData(DeviceData deviceData)
    {
        return deviceDataMapper.updateById(deviceData);
    }

    /**
     * 批量删除设备数据
     * 
     * @param ids 需要删除的设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataByIds(Long[] ids)
    {
        return deviceDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除设备数据信息
     * 
     * @param id 设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataById(Long id)
    {
        return deviceDataMapper.deleteById(id);
    }


    @Override
    public void saveMessage2DB(Message message) {
        String str = null;
        try {
            str = message.getBody(String.class);
        } catch (Exception e) {
            log.error("解析消息异常，异常的消息内容是:{}", JSONUtil.toJsonStr(message));
            return;
        }
        Map<String,Object> bodys = JSONUtil.toBean(str, Map.class);
        // 优雅
        assert bodys != null;
        Map<String,Object> notify_data = (Map<String, Object>) bodys.get("notify_data");
        assert notify_data != null;
        String o = notify_data.get("body").toString();
        ServicesDTO body = JSONUtil.toBean(o, ServicesDTO.class);

        //获取去属性
        List<DeviceData> list = new ArrayList<>();

        List<ServicesDTO.ServiceDTO> services = body.getServices();
        for (ServicesDTO.ServiceDTO service : services) {
            String eventTime = service.getEvent_time();
            LocalDateTime dateTime = LocalDateTimeUtil.parse(eventTime, "yyyyMMdd'T'HHmmss'Z'");
            Map<String, Object> properties = service.getProperties();
            Set<Map.Entry<String, Object>> entries = properties.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                DeviceData deviceData = new DeviceData();
                String valueStr = JSONUtil.toJsonStr(entry);
                deviceData.setDataValue(valueStr);
                deviceData.setAlarmTime(dateTime);
                list.add(deviceData);
            }
        }
        // 将list 集合遍历 补全deviceData 其他字段

        this.saveBatch(list);

    }
}
